/////////////////////////////////////////////////////////////////////////////
// Name:        ttsmp3settingspanel.cpp
// Purpose:     
// Author:      Julian Smart
// Modified by: 
// Created:     19/02/2009 07:09:12
// RCS-ID:      
// Copyright:   (c) Anthemion Software Limited
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

#include "tts/tts_mp3settingspanel.h"

////@begin XPM images
////@end XPM images


/*
 * wxTTSMP3SettingsPanel type definition
 */

IMPLEMENT_DYNAMIC_CLASS( wxTTSMP3SettingsPanel, wxPanel )


/*
 * wxTTSMP3SettingsPanel event table definition
 */

BEGIN_EVENT_TABLE( wxTTSMP3SettingsPanel, wxPanel )

////@begin wxTTSMP3SettingsPanel event table entries
    EVT_BUTTON( ID_APPLICATION_PREFERENCES_BROWSE_LAME_PROGRAM_LOCATION, wxTTSMP3SettingsPanel::OnApplicationPreferencesBrowseLameProgramLocationClick )

////@end wxTTSMP3SettingsPanel event table entries

END_EVENT_TABLE()

bool wxTTSMP3SettingsPanel::m_showToolTips = true;

/*
 * wxTTSMP3SettingsPanel constructors
 */

wxTTSMP3SettingsPanel::wxTTSMP3SettingsPanel()
{
    Init();
}

wxTTSMP3SettingsPanel::wxTTSMP3SettingsPanel( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create(parent, id, pos, size, style);
}


/*
 * wxTTSMP3SettingsPanel creator
 */

bool wxTTSMP3SettingsPanel::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
{
////@begin wxTTSMP3SettingsPanel creation
    wxPanel::Create( parent, id, pos, size, style );

    CreateControls();
    if (GetSizer())
    {
        GetSizer()->SetSizeHints(this);
    }
    Centre();
////@end wxTTSMP3SettingsPanel creation
    return true;
}


/*
 * wxTTSMP3SettingsPanel destructor
 */

wxTTSMP3SettingsPanel::~wxTTSMP3SettingsPanel()
{
////@begin wxTTSMP3SettingsPanel destruction
////@end wxTTSMP3SettingsPanel destruction
}


/*
 * Member initialisation
 */

void wxTTSMP3SettingsPanel::Init()
{
////@begin wxTTSMP3SettingsPanel member initialisation
    m_lameHyperlinkCtrl = NULL;
    m_lameProgramLocation = NULL;
    m_bitRateCtrl = NULL;
    m_bitRateDescriptionCtrl = NULL;
////@end wxTTSMP3SettingsPanel member initialisation
}


/*
 * Control creation for wxTTSMP3SettingsPanel
 */

void wxTTSMP3SettingsPanel::CreateControls()
{    
////@begin wxTTSMP3SettingsPanel content construction
    wxTTSMP3SettingsPanel* itemPanel1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemPanel1->SetSizer(itemBoxSizer2);

    wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer2->Add(itemBoxSizer3, 1, wxGROW, 5);

    itemBoxSizer3->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL, 5);

    wxCheckBox* itemCheckBox5 = new wxCheckBox( itemPanel1, ID_APPLICATION_PREFERENCES_GENERATE_MP3, _("&Generate MP3"), wxDefaultPosition, wxDefaultSize, 0 );
    itemCheckBox5->SetValue(false);
    itemCheckBox5->SetHelpText(_("Check to generate MP3 files."));
    if (wxTTSMP3SettingsPanel::ShowToolTips())
        itemCheckBox5->SetToolTip(_("Check to generate MP3 files."));
    itemBoxSizer3->Add(itemCheckBox5, 0, wxALIGN_LEFT|wxALL, 5);

    itemBoxSizer3->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL, 5);

    wxBoxSizer* itemBoxSizer7 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer3->Add(itemBoxSizer7, 0, wxGROW, 5);

    itemBoxSizer7->Add(5, 5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxBoxSizer* itemBoxSizer9 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer7->Add(itemBoxSizer9, 1, wxGROW, 5);

    m_lameHyperlinkCtrl = new wxHyperlinkCtrl( itemPanel1, ID_APPLICATION_PREFERENCES_GET_LAME, _("Get LAME"), _T("http://lame.sourceforge.net/links.php"), wxDefaultPosition, wxDefaultSize, wxHL_DEFAULT_STYLE );
    itemBoxSizer9->Add(m_lameHyperlinkCtrl, 0, wxALIGN_LEFT|wxALL, 5);

    wxStaticText* itemStaticText11 = new wxStaticText( itemPanel1, wxID_STATIC, _("&LAME program location:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer9->Add(itemStaticText11, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxTOP, 5);

    wxBoxSizer* itemBoxSizer12 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer9->Add(itemBoxSizer12, 0, wxGROW, 5);

    m_lameProgramLocation = new wxTextCtrl( itemPanel1, ID_APPLICATION_PREFERENCES_LAME_PROGRAM_LOCATION, wxEmptyString, wxDefaultPosition, wxSize(280, -1), 0 );
    m_lameProgramLocation->SetHelpText(_("The LAME full executable path."));
    if (wxTTSMP3SettingsPanel::ShowToolTips())
        m_lameProgramLocation->SetToolTip(_("The LAME full executable path."));
    itemBoxSizer12->Add(m_lameProgramLocation, 1, wxALIGN_CENTER_VERTICAL|wxLEFT|wxTOP|wxBOTTOM, 5);

    wxButton* itemButton14 = new wxButton( itemPanel1, ID_APPLICATION_PREFERENCES_BROWSE_LAME_PROGRAM_LOCATION, _("Browse"), wxDefaultPosition, wxDefaultSize, 0 );
    itemButton14->SetHelpText(_("Click to browse for the LAME executable."));
    if (wxTTSMP3SettingsPanel::ShowToolTips())
        itemButton14->SetToolTip(_("Click to browse for the LAME executable."));
    itemBoxSizer12->Add(itemButton14, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    itemBoxSizer9->Add(5, 5, 0, wxALIGN_CENTER_HORIZONTAL, 5);

    wxStaticText* itemStaticText16 = new wxStaticText( itemPanel1, wxID_STATIC, _("&Bit rate:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer9->Add(itemStaticText16, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxTOP, 5);

    wxBoxSizer* itemBoxSizer17 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer9->Add(itemBoxSizer17, 0, wxGROW, 5);

    wxArrayString m_bitRateCtrlStrings;
    m_bitRateCtrlStrings.Add(_("16"));
    m_bitRateCtrlStrings.Add(_("24"));
    m_bitRateCtrlStrings.Add(_("48"));
    m_bitRateCtrlStrings.Add(_("96"));
    m_bitRateCtrlStrings.Add(_("128"));
    m_bitRateCtrlStrings.Add(_("144"));
    m_bitRateCtrlStrings.Add(_("160"));
    m_bitRateCtrlStrings.Add(_("192"));
    m_bitRateCtrlStrings.Add(_("240"));
    m_bitRateCtrlStrings.Add(_("256"));
    m_bitRateCtrl = new wxChoice( itemPanel1, ID_APPLICATION_PREFERENCES_BITRATE, wxDefaultPosition, wxSize(100, -1), m_bitRateCtrlStrings, 0 );
    m_bitRateCtrl->SetHelpText(_("Choose larger numebrs for for higher quality."));
    if (wxTTSMP3SettingsPanel::ShowToolTips())
        m_bitRateCtrl->SetToolTip(_("Choose larger numebrs for for higher quality."));
    itemBoxSizer17->Add(m_bitRateCtrl, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxStaticText* itemStaticText19 = new wxStaticText( itemPanel1, wxID_STATIC, _("kbs"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer17->Add(itemStaticText19, 0, wxALIGN_CENTER_VERTICAL|wxRIGHT|wxTOP|wxBOTTOM, 5);

    itemBoxSizer17->Add(5, 5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_bitRateDescriptionCtrl = new wxStaticText( itemPanel1, wxID_STATIC, _("128kbs is the default bit rate, but for speech audio you can reduce it to 48kbs without significant loss of quality and your files will be smaller."), wxDefaultPosition, wxDefaultSize, 0 );
    m_bitRateDescriptionCtrl->Wrap(250);
    itemBoxSizer17->Add(m_bitRateDescriptionCtrl, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxBoxSizer* itemBoxSizer22 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer9->Add(itemBoxSizer22, 0, wxGROW|wxRIGHT, 5);

    itemBoxSizer22->Add(20, 5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    // Set validators
    itemCheckBox5->SetValidator( wxGenericValidator(& m_info.m_enabled) );
    m_lameProgramLocation->SetValidator( wxGenericValidator(& m_info.m_encoderPath) );
////@end wxTTSMP3SettingsPanel content construction
}


/*
 * Should we show tooltips?
 */

bool wxTTSMP3SettingsPanel::ShowToolTips()
{
    return GetShowToolTips();
}

/*
 * Get bitmap resources
 */

wxBitmap wxTTSMP3SettingsPanel::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin wxTTSMP3SettingsPanel bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end wxTTSMP3SettingsPanel bitmap retrieval
}

/*
 * Get icon resources
 */

wxIcon wxTTSMP3SettingsPanel::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin wxTTSMP3SettingsPanel icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end wxTTSMP3SettingsPanel icon retrieval
}

bool wxTTSMP3SettingsPanel::TransferDataToWindow()
{
    wxPanel::TransferDataToWindow();
    
    wxString str(wxString::Format(wxT("%d"), m_info.m_bitRate));
    m_bitRateCtrl->SetStringSelection(str);

    return true;
}

bool wxTTSMP3SettingsPanel::TransferDataFromWindow()
{
    wxPanel::TransferDataFromWindow();
    
    m_info.m_bitRate = wxAtoi(m_bitRateCtrl->GetStringSelection());
    
    return true;
}

/*
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_APPLICATION_PREFERENCES_BROWSE_LAME_PROGRAM_LOCATION
 */

void wxTTSMP3SettingsPanel::OnApplicationPreferencesBrowseLameProgramLocationClick( wxCommandEvent& WXUNUSED(event) )
{
    wxTextCtrl* textCtrl = m_lameProgramLocation;
    
    wxString msg = _("Choose a command");
    wxWindow* parent = wxGetTopLevelParent(this);
    wxString filename(textCtrl->GetValue());

    wxString file(wxFileNameFromPath(filename));
    wxString path(wxPathOnly(filename));
    wxString wildcard(wxT("*.*"));

    wxFileDialog fileDlg(parent, msg, path, file, wildcard);
    if (fileDlg.ShowModal() == wxID_OK)
    {
        path = fileDlg.GetPath();

        textCtrl->SetValue(path);
    }
}

